package com.xiaochao.bigdata.test

import java.util
import java.util.{Random, UUID}

import com.xiaochao.bigdata.util.{DateUtils, StringUtils}
import org.apache.spark.SparkContext
import org.apache.spark.sql.{Row, RowFactory, SQLContext}
import org.apache.spark.sql.types._

import scala.collection.mutable.ArrayBuffer

object MockData {
  /**
    * 弄你数据
    *
    * @param sc
    * @param sqlContext
    */
  def mock(sc: SparkContext, sqlContext: SQLContext): Unit = {
    val rows = ArrayBuffer[Row]()
    val searchKeywords = Array[String]("火锅", "蛋糕", "重庆辣子鸡", "重庆小面", "呷哺呷哺", "新辣道鱼火锅", "国贸大厦", "太古商场", "日本料理", "温泉")
    val date = DateUtils.getTodayDate()
    val actions = Array[String]("search", "click", "order", "pay")
    val random = new Random

    for (i <- 0 to 100)
    {
      val userid:String = random.nextInt(100)+""
      for (j <- 0 to 10)
      {
        val sessionid:java.lang.String = UUID.randomUUID.toString.replace("-", "")
        val baseActionTime = date + " " + random.nextInt(23)
        var k = 0
        while ( {
          k < random.nextInt(100)
        }) {
          val pageid:String  = random.nextInt(10) +""
          val actionTime:String = baseActionTime + ":" + StringUtils.fulfuill(String.valueOf(random.nextInt(59))) + ":" + StringUtils.fulfuill(String.valueOf(random.nextInt(59)))
          var searchKeyword:String = ""
          var clickCategoryId:String = ""
          var clickProductId:String = ""
          var orderCategoryIds:String = ""
          var orderProductIds:String = ""
          var payCategoryIds:String = ""
          var payProductIds:String = ""
          val action = actions(random.nextInt(4))
          if ("search" == action) searchKeyword = searchKeywords(random.nextInt(10))
          else if ("click" == action) {
            clickCategoryId = String.valueOf(random.nextInt(100))
            clickProductId = String.valueOf(random.nextInt(100))
          }
          else if ("order" == action) {
            orderCategoryIds = String.valueOf(random.nextInt(100))
            orderProductIds = String.valueOf(random.nextInt(100))
          }
          else if ("pay" == action) {
            payCategoryIds = String.valueOf(random.nextInt(100))
            payProductIds = String.valueOf(random.nextInt(100))

          }
          //          val row2:Row= RowFactory.create(date,"123")
          val row:Row = Row(date, userid, sessionid, pageid, actionTime, searchKeyword, clickCategoryId, clickProductId, orderCategoryIds, orderProductIds, payCategoryIds, payProductIds)
          rows +=row

          {
            k += 1; k - 1
          }
        }


      }


    }
    val rowsRDD = sc.parallelize(rows)
    val schema = StructType(Array(StructField("date", StringType, true), StructField("user_id", StringType, true), StructField("session_id", StringType, true), StructField("page_id",StringType, true),StructField("action_time", StringType, true), StructField("search_keyword", StringType, true), StructField("click_category_id", StringType, true), StructField("click_product_id",StringType, true), StructField("order_category_ids", StringType, true), StructField("order_product_ids", StringType, true), StructField("pay_category_ids", StringType, true), StructField("pay_product_ids",StringType, true)))
    val df = sqlContext.createDataFrame(rowsRDD, schema)
    df.createTempView("user_visit_action")

    sqlContext.sql("select count(1) from user_visit_action").show()


    rows.clear()
    val sexes = Array[String]("male", "female")
    for (i <- 0 to 99)
    {
      val userid = i + ""
      val username = "user" + i
      val name = "name" + i
      val age = random.nextInt(60)
      val professional = "professional" + random.nextInt(100)
      val city = "city" + random.nextInt(100)
      val sex = sexes(random.nextInt(2))
      val row = Row(userid, username, name, age, professional, city, sex)
      rows +=row

    }
    var rowsRDD2 = sc.parallelize(rows)
    val schema2 = StructType(Array(StructField("user_id", StringType, true), StructField("username", StringType, true), DataTypes.createStructField("name", StringType, true), StructField("age", IntegerType, true), StructField("professional", StringType, true), StructField("city", StringType, true), StructField("sex",StringType, true)))
    val df2 = sqlContext.createDataFrame(rowsRDD2, schema2)

    df2.createTempView("user_info")

    sqlContext.sql("select count(1) from user_info").show()

  }
}
